package pers.zwy.DMSystem.administratorManagement.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import pers.zwy.DMSystem.administratorManagement.dao.AdministratorDao;
import pers.zwy.DMSystem.administratorManagement.domain.Administrator;
import pers.zwy.DMSystem.util.dao.impl.BaseDaoImpl;

public class AdministratorDaoImpl extends BaseDaoImpl implements AdministratorDao {

	public String getInsertSql() {
		return "insert into t_manager (username,password,name,birthday,nation,phone,idcard,online)values(?,?,?,?,?,?,?,?)";
	}

	public void setInsertSql(Object data, PreparedStatement pstm) throws Exception {
		Administrator manager = (Administrator)data;
		pstm.setString(1, manager.getUsername());
		pstm.setString(2, manager.getPassword());
		pstm.setString(3, manager.getName());
		pstm.setDate(4, manager.getBirthday());
		pstm.setString(5, manager.getNation());
		pstm.setString(6, manager.getPhone());
		pstm.setString(7, manager.getIdcard());
		pstm.setInt(8, manager.getOnline());
		
	}
	public String getDeleteSql() {
		return "delete from t_manager where id=?";
	}

	public void setDeleteSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);
	}

	public String getUpdateSql() {
		return "update t_manager set username=?,password=?,name=?,birthday=?,nation=?,phone=?,idcard=?,qq=?,dormitory_number=?,online=? where id=?";
	}

	public void setUpdateSql(Object data, PreparedStatement pstm) throws Exception {
		Administrator manager = (Administrator)data;
		pstm.setString(1, manager.getUsername());
		pstm.setString(2, manager.getPassword());
		pstm.setString(3, manager.getName());
		pstm.setDate(4, manager.getBirthday());
		pstm.setString(5, manager.getNation());
		pstm.setString(6, manager.getPhone());
		pstm.setString(7, manager.getIdcard());
		pstm.setString(8, manager.getQq());
		pstm.setInt(9, manager.getDormitory_number());
		pstm.setInt(10, manager.getOnline());
		pstm.setInt(11, manager.getId());
	}

	public String getFindByIdSql() {
		return "select *from t_manager where id=?";
	}

	public void setFindByIdSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);
	}

	public String getFindAllSql() {
		return "select *from t_manager";
	}

	public String getFindByConditionSql(Object data) throws Exception {
		Administrator manager = (Administrator)data;
		StringBuilder sql = new StringBuilder("select *from t_manager where 1=1");
		//username=?,password=?,name=?,birthday=?,nation=?,
		//phone=?,idcard=?,qq=? where id=?
		if(manager.getId()!=null) {
			sql.append(" and id="+manager.getId());
		}
		if(manager.getUsername()!=null) {
			sql.append(" and username='"+manager.getUsername()+"'");
		}
		if(manager.getName()!=null) {
			sql.append(" and name='"+manager.getName()+"'");
		}
		if(manager.getBirthday()!=null) {
			sql.append(" and birthday='"+manager.getBirthday()+"'");
		}
		if(manager.getNation()!=null) {
			sql.append(" and nation='"+manager.getNation()+"'");
		}
		if(manager.getPhone()!=null) {
			sql.append(" and phone='"+manager.getPhone()+"'");
		}
		if(manager.getQq()!=null) {
			sql.append(" and qq='"+manager.getQq()+"'");
		}
		if(manager.getIdcard()!=null) {
			sql.append(" and idcard='"+manager.getIdcard()+"'");
		}
		if(manager.getDormitory_number()!=null) {
			sql.append(" and dormitory_number="+manager.getDormitory_number());
		}
		if(manager.getPassword()!=null) {
			sql.append(" and password='"+manager.getPassword()+"'");
		}
		if(manager.getOnline()!=null) {
			sql.append(" and online="+manager.getOnline());
		}
		return new String(sql);
	}

	public Object rsToObject(ResultSet rs) throws Exception {
		Administrator manager = new Administrator();
		manager.setBirthday(rs.getDate("birthday"));
		manager.setDormitory_number(rs.getInt("dormitory_number"));
		manager.setId(rs.getInt("id"));
		manager.setIdcard(rs.getString("idcard"));
		manager.setName(rs.getString("name"));
		manager.setNation(rs.getString("nation"));
		manager.setPassword(rs.getString("password"));
		manager.setPhone(rs.getString("phone"));
		manager.setQq(rs.getString("qq"));
		manager.setUsername(rs.getString("username"));
		manager.setOnline(rs.getInt("online"));
		return manager;
	}

}
